package org.apache.directory.studio.persistence.view.wizards;


import org.apache.directory.studio.persistence.core.CodeGeneratorContext;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;


public class DaoRequiredParametersWizardPage extends WizardPage
{
    private Text serverNameText;
    private Text portNumberText;
    private Text bindDnText;
    private Text bindPasswordText;
    private Text rdnAttributeText;
    private Text parentDnText;
    private Text objectClassesText;

    private CodeGeneratorContext codeGenContext;


    protected DaoRequiredParametersWizardPage()
    {
        super( "DaoRequiredParametersWizardPage" );
        setTitle( "LDAP Parameters" );
        setDescription( "LDAP parameters for DAO creation." );
        // setImageDescriptor( //Image to be added );
    }


    public void createControl( Composite parent )
    {
        Composite composite = new Composite( parent, SWT.NULL );
        GridLayout layout = new GridLayout( 2, false );
        composite.setLayout( layout );
        Label serverNameLabel = new Label( composite, SWT.NONE );
        serverNameLabel.setText( "Server Name:" );
        serverNameText = new Text( composite, SWT.BORDER );
        serverNameText.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
        Label portNumberLabel = new Label( composite, SWT.NONE );
        portNumberLabel.setText( "Port Number:" );
        portNumberText = new Text( composite, SWT.BORDER );
        portNumberText.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
        Label nameLabel = new Label( composite, SWT.NONE );
        nameLabel.setText( "Bind DN:" );
        bindDnText = new Text( composite, SWT.BORDER );
        bindDnText.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
        Label credentialsLabel = new Label( composite, SWT.NONE );
        credentialsLabel.setText( "Bind Password:" );
        bindPasswordText = new Text( composite, SWT.BORDER );
        bindPasswordText.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
        Label rdnAttributeLabel = new Label( composite, SWT.NONE );
        rdnAttributeLabel.setText( "RDN Attribute:" );
        rdnAttributeText = new Text( composite, SWT.BORDER );
        rdnAttributeText.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
        Label parentDnLabel = new Label( composite, SWT.NONE );
        parentDnLabel.setText( "Parent DN:" );
        parentDnText = new Text( composite, SWT.BORDER );
        parentDnText.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
        Label objectClassesLabel = new Label( composite, SWT.NONE );
        objectClassesLabel.setText( "Object Classes:" );
        objectClassesText = new Text( composite, SWT.BORDER );
        objectClassesText.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
        initFields();

        serverNameText.addModifyListener( new ModifyListener()
        {
            public void modifyText( ModifyEvent e )
            {
                dialogChanged();
            }
        } );

        portNumberText.addModifyListener( new ModifyListener()
        {
            public void modifyText( ModifyEvent e )
            {
                dialogChanged();
            }
        } );

        bindDnText.addModifyListener( new ModifyListener()
        {
            public void modifyText( ModifyEvent e )
            {
                dialogChanged();
            }
        } );

        bindPasswordText.addModifyListener( new ModifyListener()
        {
            public void modifyText( ModifyEvent e )
            {
                dialogChanged();
            }
        } );

        rdnAttributeText.addModifyListener( new ModifyListener()
        {
            public void modifyText( ModifyEvent e )
            {
                dialogChanged();
            }
        } );

        parentDnText.addModifyListener( new ModifyListener()
        {
            public void modifyText( ModifyEvent e )
            {
                dialogChanged();
            }
        } );
        objectClassesText.addModifyListener( new ModifyListener()
        {
            public void modifyText( ModifyEvent e )
            {
                dialogChanged();
            }
        } );

        setControl( composite );

    }


    private void initFields()
    {
        serverNameText.setText( codeGenContext.getServer() );
        portNumberText.setText( String.valueOf( codeGenContext.getPort() ) );
        bindDnText.setText( codeGenContext.getBindDn() );
        bindPasswordText.setText( codeGenContext.getBindPassword() );
        rdnAttributeText.setText( codeGenContext.getRdnAttribute() );
        parentDnText.setText( codeGenContext.getParentDn() );
        for ( String objectClass : codeGenContext.getObjectClasses() )
        {
            objectClassesText.append( objectClass + "," );
        }
    }


    private void dialogChanged()
    {
        codeGenContext.setServer( serverNameText.getText() );
        codeGenContext.setPort( Integer.parseInt( portNumberText.getText() ) );
        codeGenContext.setBindDn( bindDnText.getText() );
        codeGenContext.setBindPassword( bindPasswordText.getText() );
        codeGenContext.setRdnAttribute( rdnAttributeText.getText() );
        codeGenContext.setParentDn( parentDnText.getText() );
        String[] objClassOut = objectClassesText.getText().split( "," );
        codeGenContext.setObjectClasses( objClassOut );
    }


    public void setCodeGenContext( CodeGeneratorContext codeGenContext )
    {
        this.codeGenContext = codeGenContext;
    }
}
